package top.fangw.tenant.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import top.fangw.common.core.constant.BaseConstant;
import top.fangw.common.core.exception.BizException;
import top.fangw.common.mybatis.annotation.DyDataSource;
import top.fangw.common.mybatis.service.BaseTreeService;
import top.fangw.tenant.mapper.MenuInfoMapper;
import top.fangw.tenant.pojo.dto.ApplicationPageQueryDto;
import top.fangw.tenant.pojo.entity.ApplicationInfo;
import top.fangw.tenant.mapper.ApplicationInfoMapper;
import top.fangw.tenant.pojo.entity.MenuInfo;
import top.fangw.tenant.service.IApplicationInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.fangw.tenant.service.IMenuInfoService;

import java.util.List;

/**
 * <p>
 * 用途：用于管理系统中的应用信息 服务实现类
 * </p>
 *
 * @author fangw
 * @since 2024-02-18
 */
@Service
@RequiredArgsConstructor
public class ApplicationInfoServiceImpl extends ServiceImpl<ApplicationInfoMapper, ApplicationInfo> implements IApplicationInfoService {
    private final ApplicationInfoMapper applicationInfoMapper;
    private final MenuInfoMapper menuInfoMapper;


    @Override
    @DyDataSource
    public void add(ApplicationInfo applicationInfo) {
        BaseTreeService<ApplicationInfoMapper, ApplicationInfo> treeService = new BaseTreeService<>(applicationInfoMapper);
        treeService.saveOrUpdateTree(applicationInfo);
    }

    @Override
    public void delete(String id) {
        //判断其是否有子菜单，如果有则不能够删除
        QueryWrapper<ApplicationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("parent_ids", id + BaseConstant.COMMA);
        Long count = applicationInfoMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new BizException("该应用下有子应用，不能够删除");
        } else {
            //删除应用信息
            applicationInfoMapper.deleteById(id);
        }
    }

    @Override
    public IPage<ApplicationInfo> getTreeList(ApplicationPageQueryDto applicationPageQueryDto) {
        LambdaQueryWrapper<ApplicationInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(applicationPageQueryDto.getName())) {
            queryWrapper.like(ApplicationInfo::getName, applicationPageQueryDto.getName());
        }
        if (StrUtil.isNotBlank(applicationPageQueryDto.getSysSimpleName())) {
            queryWrapper.like(ApplicationInfo::getSysSimpleName, applicationPageQueryDto.getSysSimpleName());
        }
        queryWrapper.orderByAsc(ApplicationInfo::getShowSort);
        BaseTreeService<ApplicationInfoMapper, ApplicationInfo> treeService = new BaseTreeService<>(applicationInfoMapper);
        return treeService.pageList(queryWrapper, applicationPageQueryDto.getCurrent(), applicationPageQueryDto.getSize());
    }

    @Override
    public ApplicationInfo getDetail(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public List<ApplicationInfo> getParentApplication() {
        LambdaQueryWrapper<ApplicationInfo> queryWrapper = new LambdaQueryWrapper<ApplicationInfo>().eq(ApplicationInfo::getShowFlag, "1").orderByAsc(ApplicationInfo::getShowSort);
        BaseTreeService<ApplicationInfoMapper, ApplicationInfo> treeService = new BaseTreeService<>(applicationInfoMapper);
        return treeService.treeList(queryWrapper);
    }

    @Override
    public List<ApplicationInfo> getApplicationAndMenu() {
        List<ApplicationInfo> applicationInfoList = getParentApplication();
        addMenuInfo(applicationInfoList);
        // 查询应用信息
        return applicationInfoList;
    }

    private void addMenuInfo(List<ApplicationInfo> list) {
        list.forEach(item -> {
            if (item.getChildren() == null || item.getChildren().size() == 0) {
                // 查询并添加菜单信息
                LambdaQueryWrapper<MenuInfo> queryWrapper = new LambdaQueryWrapper<MenuInfo>().eq(MenuInfo::getApplicationId,item.getId()).orderByAsc(MenuInfo::getSort);
                BaseTreeService<MenuInfoMapper, MenuInfo> treeService = new BaseTreeService<>(menuInfoMapper);
                item.setChildren(treeService.treeList(queryWrapper));
                System.out.println("item = " + treeService.treeList(queryWrapper));
            } else {
                addMenuInfo(item.getChildren());
            }
        });
    }
}
